home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12017 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  67 lines

  1. Newsgroups: comp.lang.c
  2. Path: leeds.ac.uk!news
  3. From: csyamc@scs.leeds.ac.uk (A M Casey)
  4. Subject: malloc help needed
  5. Message-ID: <1996Mar28.113702.15788@leeds.ac.uk>
  6. NNTP-Posting-Host: csgi08.leeds.ac.uk
  7. Organization: The University of Leeds, School of Computer Studies
  8. Date: Thu, 28 Mar 1996 11:37:02 +0000 (GMT)
  9.  
  10. As requested, here is the code that I cant suss out. I think the problem might
  11. be something to do with allocating memory in different functions, 
  12.  
  13. void PositionSelectCallback(Widget w,
  14.                             XtPointer clientData,
  15.                             XtPointer callData)
  16.  
  17. /* a standard callback function in X/motif*/
  18.  
  19. char *ItemName;
  20. /* stuff deleted */
  21. ItemName=malloc(20);
  22. XmStringGetLtoR(ItName, XmSTRING_DEFAULT_CHARSET, &ItemName);
  23. /*this assigns a string to ItemName (works fine) */
  24.  
  25. ItemSelectCallback(ItemName,0);
  26.  
  27. /* calls another function */
  28.  
  29.  
  30. void ItemSelectCallback(char *ItemName,
  31.                             int Flag)
  32.  
  33. int NoOfItems;
  34. char *Item[20];
  35.  
  36. printf("the name is fine -%s\n",ItemName);
  37. NoOfItems = GetInfoFromFile(Item,ItemName,Flag);
  38. printf("the name is not ok now - %s\n",ItemName);
  39.  
  40.  
  41. now then, obviously GetInfoFromFile changes ItemName, or rather
  42. the area of memory it points to, but I have not changed it in any
  43. way, it is just for comparisons with other strings read in.....
  44.  
  45. int GetInfoFromFile(char *Item[20],char *ItemName,int Flag)
  46.  char *tempcontents[50];
  47.  
  48. for(count = 0; count <=20;count++)
  49. {
  50. Item[count] = malloc(50);
  51. }
  52. for(count = 0; count <=50;count++)
  53. {
  54. tempcontents[count] = malloc(50);
  55. }
  56.  
  57.  
  58. The only dodgy thing is that I'm declaring Item and allocating
  59. memory to it in a different function, but as far as I know this
  60. shouldnt be a problem. Everything else in the functions is stuff
  61. I've done for years, and it all works fine, it's just this memory
  62. allocation which seems to be messing up.
  63.  
  64. Cheers for your help
  65.  
  66. Andy
  67.